package com.orangelife.mobile.util;

import android.app.Dialog;

import com.curry.android.adapter.FatherAdapter;
import com.curry.android.util.JSONHelper;
import com.google.gson.Gson;
import org.json.JSONException;
import org.json.JSONObject;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

public class JsonReponseHandler {

	// private static final String TAG =
	// JsonReponseHandler.class.getSimpleName();

	/**
	 * json中带pageEntity的格式，转为list
	 *
	 * @param <T>
	 *
	 * @param response
	 *            服务端返回的JSON格式数据
	 * @return json转成的list
	 */
	public static <T> List<T> getListFromJson(Object response, Type type) {
		return getListFromJson((JSONObject) response, type);
	}
	public static <T> List<T> getListFromJson(Object response, Type type,Dialog dialog) {
		return  getListFromJson(response,"pageEntity","list",type, null,dialog);
	}

	/** json中带pageEntity和llist */
	public static <T> List<T> getListFromJson(JSONObject response, Type type) {
		return getListFromJson(response, "pageEntity", "list", type, null, null);
	}

	/** 不带pageEntity，自定义list名 */
	public static <T> List<T> getListFromJson(String listName,
											  JSONObject response, Type type) {
		return getListFromJson(response, null, listName, type, null, null);
	}

	/** json中带pageEntity和list，还有adapter和dialog，没有List<T> */
	public static <T> List<T> getListFromJson(Object response, Type type,
											  FatherAdapter<T> baseAdapter, Dialog dialog) {
		return getListFromJson(response, type, null, baseAdapter, dialog);
	}

	/** json中带pageEntity和list，还有adapter和dialog */
	public static <T> List<T> getListFromJson(Object response, Type type,
											  List<T> list, FatherAdapter<T> baseAdapter, Dialog dialog) {
		return getListFromJson(response, "pageEntity", "list", type,
				baseAdapter, dialog);
	}

	/**
	 * 将服务器返回的JSON转化为List
	 *
	 * @param response
	 *            服务器的HTTP响应，JSON数据，之所以用object，是希望将msg.obj直接传送过来
	 * @param type
	 *            List的数据类型，如List<Comment>
	 * @param baseAdapter
	 *            更新ListView的适配器
	 * @param dialog
	 *            进度对话框
	 * @author 陈福荣,吴格非
	 */
	public static <T> List<T> getListFromJson(Object response, String page,
											  String listName, Type type, FatherAdapter<T> baseAdapter,
											  Dialog dialog) {
		if (response == null) {
			return null;
		}
		JSONObject jo = null;
		if (response instanceof JSONObject) {
			jo = (JSONObject) response;
		}else if (response instanceof String) {
			try {
				jo = new JSONObject(response.toString());
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}
		Gson gson = new Gson();
		List<T> list = null;
		try {
			// 如果有对话框，则关闭对话框
			if (dialog != null&&dialog.isShowing()) {
				dialog.dismiss();
			}

			// 取list
			if(jo!=null && !jo.equals("")&&(jo!=null&& !jo.equals(""))){
				String json=jo.getJSONArray(page).toString();
				if(!"null".equals(json)){//服务端可能返回null
					list = gson.fromJson(json, type);
				}
				if (list == null) {
					list = new ArrayList<T>();
				}
			}
			if (baseAdapter != null) {// 如果有传入adapter，直接刷新界面
				baseAdapter.setList(list);
				baseAdapter.notifyDataSetChanged();
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return list;
	}

	/** json中带pageEntity和list，还有adapter和dialog */

	public static <T> List<T> getListFromJson(String json,
											  FatherAdapter<T> baseAdapter, Dialog dialog) {
		if(json.equals("500")){
			if (dialog != null&&dialog.isShowing()) {
				dialog.dismiss();
			}
			ToastHelper.getInstance()._toast("获取数据出错！");
			return null;
		}
		return getListFromJson(json, "pageEntity", "list",
				baseAdapter, dialog);
	}

	/**
	 * 将服务器返回的JSON转化为List
	 *
	 * @param response
	 *            服务器的HTTP响应，JSON数据，之所以用object，是希望将msg.obj直接传送过来
	 * @param baseAdapter
	 *            更新ListView的适配器
	 * @param dialog
	 *            进度对话框
	 * @author 陈翔
	 */
	@SuppressWarnings("unchecked")
	public static <T> List<T> getListFromJson(Object response, String page,
											  String listName, FatherAdapter<T> baseAdapter,
											  Dialog dialog) {
		if (response == null) {
			return null;
		}
		JSONObject jo = null;
		if (response instanceof JSONObject) {
			jo = (JSONObject) response;
		}else if (response instanceof String) {
			try {
				jo = new JSONObject(response.toString());
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}
		List<T> list = null;
		try {
			// 如果有对话框，则关闭对话框
			if (dialog != null&&dialog.isShowing()) {
				dialog.dismiss();
			}

			// 取list
			if(page != null && !page.equals("")&& !jo.equals("")){
				if(jo.get(page).toString().equals("")||jo.get(page).toString().equals("null")){
					return list;
				}
				String json=jo.getJSONArray(page).toString();


				if(!"null".equals(json)){//服务端可能返回null
					list=(List<T>) JSONHelper.jsonToMap(response.toString()).get(page);

				}
				if (list == null) {
					list = new ArrayList<T>();
				}
				if (baseAdapter != null) {// 如果有传入adapter，直接刷新界面
					baseAdapter.setList(list);
					baseAdapter.notifyDataSetChanged();
				}
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return list;
	}
}
